 <!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <title>{$Think.config.site.title} - 库存管理</title>

    <meta name="keywords" content="{$Think.config.site.keywords}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">

</head>
<style>
#listtable .fa{
	cursor:pointer;
	color:#1ab394;
	margin-right:1em;
}
th .tag{font-size: 12px; color: #e66c6c; font-weight: normal;}
.ibox-title .tag{font-size: 12px; color: #e66c6c; font-weight: normal; display: inline-block; margin-left: 20px;}
.specval-hide{display: none;}


.table>tbody>tr.active>td{background: rgb(26, 179, 148) !important}
.error{background: #f00}
</style>
<body class="gray-bg">
	<form method="post" class="form-horizontal">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
            <div class="col-sm-4">
                <div>
                	<button type="button" class="btn btn-primary btn-sm" onclick="window.history.go(-1);">返回</button>
                    <button type="button" class="btn btn-primary" id="submit" ><i class="fa fa-sign-in"></i> 出库</button>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="input-group">
                    <input type="text" name="scancode" id="scancode" class="input-sm form-control">
                    <span class="input-group-btn"> <a class="btn btn-sm btn-primary" id="entertype"> 切换到手动模式</a> </span>
                </div>
            </div>
      </div>
      
      <div class="row">
          <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>商品入库</h5>
                        <!-- <span class="tag">说明：</span> -->
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                        	<div class="col-sm-6">
                        	</div>
                          	<div class="col-sm-6">
                                <div class="input-group pull-right">
               
                                </div>
                            </div>
                            
                        </div>
                        <div class="table-responsive">
                            <table class="table" id="listtable">
                                <thead>
                                    <tr>
                                        <th width="150">商品_规格ID</th>
                                        <th >商品图片</th>
                                        <th >商品名称</th>
                                        <th >商品价格</th>
                                        <th >订单商品数量</th>
                                        <th >出库数量</th>
                                    </tr>
                                </thead>
                                <tbody>

                                	{volist name="list" id="vo" key="m"}
                                    <tr data-specval_id="{$vo.goods_specval_id}">

                                        <td>{$vo.goods_id}_{$vo.goods_specval_id}</td>
                                        <td><img src="{$vo.goods_picture}" width="30" height="30"></td>
                                        <td>{if condition="$vo.brand_name!=''"}（{$vo.brand_name}）{/if}{$vo.goods_name}【{$vo.goods_specval_name}】</td>
                                        <td>{$vo.price}</td>
                                        <td class="num">{$vo.num}</td>
                                        <td><input type="text" name="outstock_num" class="form-control outstock_num" placeholder="本次出库数量" autocomplete="off"></td>
                                    </tr>
                                   	{/volist}

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        
    </div>
    </form>
    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/peity/jquery.peity.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/iCheck/icheck.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/majax.js"></script>
    <script src="{$Think.config.site.common_resource_url}js/jedit.js"></script>

    <script src="{$Think.config.site.resource_url}js/plugins/validate/jquery.validate.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/validate/messages_zh.min.js"></script>
</body>

</html>
<script>

var orderId={$order_id};
var layer_font='';

$('#submit').click(function(){
    $("form").submit();
})

function save(){
	var sign=false;
    $('#listtable tbody tr').each(function(){
    	var num = parseInt($(this).find('.num').text()),
    	outstock = parseInt($(this).find('.outstock_num').val());
    	if (num==outstock) {
    		sign = true;
    	}else{
    		sign = false;
    		return true;
    	}
    })

    if (sign) {
    	majax("{:url('outstock')}",{ids:orderId},function(re){
    		if (re.code <= 0) {
    			layer.msg(re.msg,{icon:2});
    		}else{
    			autoPrint(re.data.shippingId);
    		}
    	});
    }else{
    	layer.msg('出库数量与订单数量不匹配',{icon:5});
    }
}

$("form").validate({
	debug:false,
	submitHandler:function(form){
		save();
	},    
	rules: {
		outstock_num: {
			digits:true,
			min:1
		},
	},
	messages: {
		outstock_num: {
			digits: '数量必须是整数',
			min:'数量不能小于1'
		},
	}
});

$('#scancode').keypress(function(e){
    var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    if (eCode == 13){
        var code = $(this).val();
        $(this).val('');
        addnum(parseInt(code))
        
    }
})

function addnum(code) {
    var tr = $('tr[data-specval_id="'+code+'"]');
    if (tr.length<1) {
    	scanError()
        return false;
    }
    $(document).scrollTop(tr.offset().top-200);
    tr.addClass('active').siblings('tr').removeClass('active');
    setTimeout(function(){
        tr.removeClass('active');
    },1000)
    var num = 1
    if (tr.find('.outstock_num').val()) {
       num = parseInt(tr.find('.outstock_num').val())+num;
    }

    if (parseInt(tr.find('.num').text()) < num ) {
    	scanError()
        return false;
    }else{
    	tr.find('.outstock_num').val(num);
    }
}

function scanError(){
	$('.ibox-content').addClass('error');
	setTimeout(function(){
	    $('.ibox-content').removeClass('error');
	},1000);
}

var entertype = 0; // 扫描模式 
var time = setInterval(function(){
    // $('#scancode').focus();
    scancodefocus();
},1000);
$('#entertype').click(function(){
    if (entertype==0) {
        clearInterval(time);
        entertype=1;
        $(this).text('切换到扫码模式');
        $(this).removeClass('btn-primary').addClass('btn-warning')
    }else{
        entertype=0;
        $(this).text('切换到手动模式');
        $(this).removeClass('btn-warning').addClass('btn-primary')
        time = setInterval(function(){
            // $('#scancode').focus();
            scancodefocus();
        },1000);
    }
})
$(document).click(function(){
    if (entertype==0) {
        // $('#scancode').focus();
        scancodefocus();
    }
})

function scancodefocus(){
    if ($(window.frameElement).css('display')=='inline') {
        $('#scancode').focus();
    }
}

function autoPrint(id) {
    window.location.href="{:url('outstock')}";
    // layer_font = layer.open({
    //     type: 2,
    //     title: '打印',
    //     shadeClose: true,
    //     shade: 0.8,
    //     end: function(){window.location.reload(true)},
    //     area: ['900px', '90%'],
    //     content: '{:url("webPrintf")}?ids='+id, //iframe的url
    // });
}
function closeIfram(){
    window.location.href="{:url('outstock')}";
}
</script>